package view;

import service.BookServiceImpl;
import entity.Book;
import tool.MyTool;
import java.util.List;

public class BookView {
   private BookServiceImpl bookService = new BookServiceImpl();
    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_PURPLE = "\u001B[35m";
    public static final String ANSI_BLUE = "\u001B[34m";
    public static final String ANSI_CYAN = "\u001B[36m";
    /**
     * 书本查询与修改界面
     * @throws Exception
     */
    public void selectAndModify() throws Exception {
        while (true) {
            System.out.print(ANSI_RESET + ANSI_GREEN);
            System.out.println("🔣-------~查询与修改~-----------🔣");
            System.out.println("🐕-------<1.通过ISBN编号查询>-----*");
            System.out.println("🐖-------<2.通过书本名查找>-------*");
            System.out.println("🐱-------<3.通过书本类型查找>-----*");
            System.out.println("🐱-------<4.通过作者查找>--------*");
            System.out.println("🐕-------<5.通过出版社查找>------*");
            System.out.println("🐖-------<6.查询全书本>---------*");
            System.out.println("🐱-------<7.退出>--------------*");
            char c = MyTool.selectMenu(7).charAt(0);
            switch (c) {
                case '1':
                    Book book = bookService.getBookByISBN();
                    if (book != null) {
                        modifyView(book, null);
                    }
                    break;
                case '2':
                    Book book1 = bookService.getBookTypeByName();
                    if (book1 != null) {
                        modifyView(book1, null);
                    }
                    break;
                case '3':
                    List<Book> bookByType = bookService.getBookByType();
                    if (bookByType != null) {
                        modifyView(null, bookByType);
                    }
                    break;
                case '4':
                    List<Book> bookByAuthor = bookService.getBookByAuthor();
                    if (bookByAuthor != null) {
                        modifyView(null, bookByAuthor);
                    }
                    break;
                case '5':
                    List<Book> bookByPublish = bookService.getBookByPublish();
                    if (bookByPublish != null) {
                        modifyView(null, bookByPublish);
                    }
                    break;
                case '6':
                    List<Book> all = bookService.getAll();
                    modifyView(null, all);
                    break;
                case '7':
                    return;
            }
        }
    }

    /**
     * 修改界面
     * @param book
     * @param arr
     */
    public void modifyView(Book book, List<Book> arr) {
        System.out.println(ANSI_RESET + ANSI_GREEN);
        System.out.println("🔣-------~修改~-------🔣");
        System.out.println("🐕-------<1.修改>------*");
        System.out.println("🐖-------<2.退出>----*");
        char c = MyTool.selectMenu(2).charAt(0);
        if (c == '2') {
            return;
        }
        Book book1 = book;
        if (book == null) {
            MyTool.printArr(arr);
            System.out.println("请选择你要修改的书本序号(直接按确定退出)：");
            Integer bookIndex = MyTool.getTypeIndex(arr.size());
            if (bookIndex == null) {
                System.out.println("退出成功");
                return;
            }
            book1 = arr.get(bookIndex);
        }
        try {
            bookService.update(book1.getISBN());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 书本主界面
     * @throws Exception
     */
   public void BookMenu() throws Exception {
       System.out.print(ANSI_RESET + ANSI_GREEN);
       System.out.println("🔣-------~图书管理~-----------🔣");
       System.out.println("🐕-------<1.添加图书>-----*");
       System.out.println("🐖-------<2.查询及修改图书>-------*");
       System.out.println("🐱-------<3.删除图书>-----*");
       System.out.println("🐱-------<4.退出>-----*");

       char c = MyTool.selectMenu(4).charAt(0);
       switch (c){
           case '1':
               bookService.insert();
               break;
           case '2':
               selectAndModify();
               break;
           case '3':
               bookService.deleteByISBN();
               break;
           case '4':
               return;
       }
   }


    public static void main(String[] args) throws Exception {
        BookView bookView = new BookView();
        while (true) {
         bookView.BookMenu();
        }
    }
}
